<?php
	include 'ZionHeader.inc';
?>

<html>
<body>
	<link rel="stylesheet" href="test.css" type="text/css" />
	<?php
		$name = $_SESSION['UName'];
		$password = $_POST['password'];
		//Need to compare $name with username of announcement
	?>
<div class ="ex">
<h1>Webpage Statistics!</h1>
<?php

$Vehicle = $_POST['VehicleID'];
$RegUser = $_POST['RegUserID'];
$Ann = $_POST['AnnID'];
$Timestamp = date(r);
//echo date(r);
//echo $Vehicle;
//echo $RegUser;
//echo $Ann;

include 'DatabaseConnect.inc';

$queryV = "Select Count(*) FROM User";
mysql_query($queryV);

$queryV = "Select Count(*) AS UserCount FROM User";
$resultV = mysql_query($queryV);
$rowV = mysql_fetch_array($resultV);
echo "<ul>";
echo "<li class ='statistics'>Number of Users: ".$rowV['UserCount']."</li>";
echo "<br>";

$queryV = "Select Count(*) AS RegCount FROM RegisteredUser";
$resultV = mysql_query($queryV);
$rowV = mysql_fetch_array($resultV);

echo "<li class ='statistics'>Number of Registered Users: ".$rowV['RegCount']."</li>";
echo "<br>";

$queryV = "Select Count(*) AS DealerCount FROM Dealer";
$resultV = mysql_query($queryV);
$rowV = mysql_fetch_array($resultV);

echo "<li class ='statistics'>Number of Dealers: ".$rowV['DealerCount']."</li>";
echo "<br><br>";

$queryV = "Select Count(*) AS VehicleCount FROM Vehicle";
$resultV = mysql_query($queryV);
$rowV = mysql_fetch_array($resultV);

echo "<li class ='statistics'>Number of Vehicles: ".$rowV['VehicleCount']."</li>";
echo "<br>";

// query for make of cars
$queryMake = "SELECT * , Count(Make) AS MakeCnt FROM Vehicle GROUP BY Make ORDER BY Make ASC   " ;
$tableMake = mysql_query($queryMake);
echo "<br>";
while($rowMake = mysql_fetch_array( $tableMake )){	
	echo "<li class ='statistics'> Number of  ".$rowMake['Make']."s"."</li>";
	echo "<li class ='statistics'># of that Make: ".$rowMake['MakeCnt']."</li>" ;
	echo "<br>";
}
echo "<br>";
// query for most bookmarked car
$queryBook = "SELECT * , Count(announceID) AS AnnCnt FROM Bookmark GROUP BY announceID ORDER BY AnnCnt DESC Limit 3" ;
$tableBook = mysql_query($queryBook);
echo "<li class ='statistics'>Most Bookmarked on Vehicles "."</li>";
$i = 1;
while($rowBook = mysql_fetch_array( $tableBook )){	
	$queryAnn = "Select * FROM Announcement WHERE ID ='".$rowBook['announceID']."' ";
	$resultAnn = mysql_query($queryAnn);
	$rowAnn = mysql_fetch_array($resultAnn);
	
	$queryVeh = "SELECT * FROM Vehicle WHERE ID ='".$rowAnn['vehicle_ID']."' ";
	$resultVeh = mysql_query($queryVeh);
	$rowVeh = mysql_fetch_array($resultVeh);
	
	echo "<br><li class ='statistics'>".$i.") Announcement: ID ".$rowBook['announceID']."</li>" ;
	echo "<li class ='statistics'>Make: ".$rowVeh['Make']."</li> ";
	echo "<li class ='statistics'>Model: ".$rowVeh['Model']."</li>" ;
	echo "<li class ='statistics'>Year: ".$rowVeh['Year']."</li> ";
	echo "<li class ='statistics'># of Bookmarks: ".$rowBook['AnnCnt']."</li>" ;
	echo "<br>";
	$i+=1;
}

// query for most bookmarked car
$queryComm = "SELECT * , Count(VehicleID) AS CommCnt FROM Comment GROUP BY VehicleID ORDER BY CommCnt DESC Limit 3" ;
$tableComm = mysql_query($queryComm);
$i = 1;
echo "<br><li class ='statistics'>Most Commented on Announcements "."</li>";
while($rowComm = mysql_fetch_array( $tableComm ))
{	
	$queryVeh = "SELECT * FROM Vehicle WHERE ID ='".$rowComm['VehicleID']."' ";
	$resultVeh = mysql_query($queryVeh);
	$rowVeh = mysql_fetch_array($resultVeh);
	echo "<br><li class ='one'>".$i.")  Vehicle: ID ".$rowComm['VehicleID']."</li>" ;
	echo "<li class ='statistics'>Make: ".$rowVeh['Make']."</li>" ;
	echo "<li class ='statistics'>Model: ".$rowVeh['Model']."</li>" ;
	echo "<li class ='statistics'>Year: ".$rowVeh['Year']."</li>" ;
	echo "<li class ='statistics'># of Comments: ".$rowComm['CommCnt']."</li>" ;
	echo "<br>";
	$i +=1;
}

$queryV = "Select * FROM Vehicle ";
$tableV = mysql_query($queryV);
$TotMilf = 0;
while($rowV = mysql_fetch_array( $tableV ))
{	$TotMilf += $rowV['Mileage']; }
echo "<br><li class ='statistics'>Total mileage of all cars: ".$TotMilf."</li>";
	echo "<br></ul>";
	


mysql_close();
?>

<br />
<br />
<a href="index.php">Return To Home Page</a>
</div>

</body>
</html>

